from PyQt4 import QtCore, QtGui
from ui_contactdialog import Ui_ContactDialog
import sys
import pickle

class ContactDialog(QtGui.QDialog):
    def __init__(self):
        QtGui.QDialog.__init__(self)

        self.ui = Ui_ContactDialog()
        self.ui.setupUi(self)

        self.contacts = [{}]
        self.load_contacts()

        self.connect(self.ui.contactsComboBox, QtCore.SIGNAL("currentIndexChanged(int)"),
                     self.load_current_contact)
        self.connect(self.ui.newPushButton, QtCore.SIGNAL("clicked()"),
                     self.new_clicked)
        self.connect(self.ui.buttonBox, QtCore.SIGNAL("accepted()"),
                     self.ok_clicked)
        self.connect(self.ui.buttonBox, QtCore.SIGNAL("rejected()"),
                     self.reject)

        self.current_index = 0

    def load_contacts(self):
        try:
            file = open('contacts.dat')
            self.contacts = pickle.load(file)
            self.ui.contactsComboBox.clear()

            index = 0
            for contact in self.contacts:
                self.ui.contactsComboBox.addItem('%d - %s - %s' % (index, 
                                                                   contact['person'],
                                                                   contact['organization']))
                index = index + 1

            if index > 0:
                self.load_current_contact(0)

            file.close()

        except IOError:
            print 'Database file not found.'

    def load_current_contact(self, index):
        self.current_index = index
        contact = self.contacts[index]
        self.ui.organizationLineEdit.setText(contact['organization'])
        self.ui.personLineEdit.setText(contact['person'])

        type_index = self.ui.addresstypeComboBox.findText(contact['addresstype'])
        self.ui.addresstypeComboBox.setCurrentIndex(type_index)

        self.ui.addressTextEdit.setText(contact['address'])

        self.ui.cityLineEdit.setText(contact['city'])
        self.ui.stateLineEdit.setText(contact['state'])
        self.ui.postalcodeLineEdit.setText(contact['postalcode'])
        self.ui.countryLineEdit.setText(contact['country'])
        self.ui.voiceLineEdit.setText(contact['voice'])
        self.ui.faxLineEdit.setText(contact['fax'])
        self.ui.emailLineEdit.setText(contact['email'])

    def save_contact(self, index):
        file = open('contacts.dat', 'w')

        if index + 1 > len(self.contacts):
            self.contacts.append({})

        self.contacts[index]['organization'] = self.ui.organizationLineEdit.text()
        self.contacts[index]['person'] = self.ui.personLineEdit.text()
        self.contacts[index]['addresstype'] = self.ui.addresstypeComboBox.currentText()
        self.contacts[index]['address'] = self.ui.addressTextEdit.toPlainText()
        self.contacts[index]['city'] = self.ui.cityLineEdit.text()
        self.contacts[index]['state'] = self.ui.stateLineEdit.text()
        self.contacts[index]['postalcode'] = self.ui.postalcodeLineEdit.text()
        self.contacts[index]['country'] = self.ui.countryLineEdit.text()
        self.contacts[index]['voice'] = self.ui.voiceLineEdit.text()
        self.contacts[index]['fax'] = self.ui.faxLineEdit.text()
        self.contacts[index]['email'] = self.ui.emailLineEdit.text()
        
        pickle.dump(self.contacts, file)
        file.close()
        self.load_contacts()

    def new_clicked(self):
        self.save_contact(self.current_index)

        self.ui.contactsComboBox.addItem("")
        self.ui.organizationLineEdit.clear()
        self.ui.personLineEdit.clear()
        self.ui.addressTextEdit.clear()
        self.ui.cityLineEdit.clear()
        self.ui.stateLineEdit.clear()
        self.ui.postalcodeLineEdit.clear()
        self.ui.countryLineEdit.clear()
        self.ui.voiceLineEdit.clear()
        self.ui.faxLineEdit.clear()
        self.ui.emailLineEdit.clear()

        self.current_index = len(self.contacts)

    def ok_clicked(self):
        self.save_contact(self.current_index)
        self.emit(QtCore.SIGNAL("changed()"))
        self.hide()
        
if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = ContactDialog()
    window.show()
    sys.exit(app.exec_())
